package com.similarworld.bean;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.similarworld.model.User;
import com.similarworld.service.UserService;
import com.similarworld.type.UserStatus;
import com.similarworld.type.UserType;

@Component("userBean")
@Scope("view")
public class UserBean {

	private static final Logger logger = LoggerFactory.getLogger(UserBean.class);

	private User user = new User();
	private List<User> users;

	@Autowired
	private UserService service;

	public User getUser() {
		return user;
	}

	public void save() {
		user.setCreateUserManually(true);
		user.setStatus(UserStatus.ACTIVE);
		user.setType(UserType.SUPERUSER);
		service.save(user);
		logger.debug("User created"+user.getUsername());
		user = new User();
		users = null;
	}

	public List<User> getUsers() {
		if (users == null) {
			users = service.getAll();
		}
		return users;
	}
}
